//
//  SearchVC.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2018/12/24.
//  Copyright © 2018 Gox_WallyDiao. All rights reserved.
//

import UIKit

class SearchVC: TTBaseVC {
    
    var searchStr = ""
    
    private let SearchListTableViewCellID = "ListenTableViewCell"
    
    var listModelArr:[ListenListModel] = [ListenListModel]()
    
    lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.bounces = true
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(SearchListTableViewCellID), forCellReuseIdentifier: SearchListTableViewCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        return tableView
    }()
    
    
    lazy var searchBar: UISearchBar = {
        let tempSearchBar = UISearchBar(frame:CGRect(x: 0, y: 0, width: SCREEN_WIDTH - 60, height: 40))
        tempSearchBar.searchBarStyle = .minimal
        tempSearchBar.placeholder = "请输入搜索关键字";
        tempSearchBar.backgroundColor = WhiteBackColor
        tempSearchBar.showsCancelButton = false
        tempSearchBar.delegate = self
        
        return tempSearchBar
        }()
    
    lazy var searchBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH - 60, 0, 60, 40)
        button.setTitle("搜索", for: .normal)
        button.addTarget(self, action: #selector(searchBtnAction), for: .touchUpInside)
        button.setTitleColor(UIColor.lightGray, for: .normal)
        button.titleLabel?.font = FONT(17)
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "搜索结果"
        
        self.view.backgroundColor = WhiteBackColor
        
        self.view.addSubview(tableView)
        
        setHeaderView()
//        setRecommendView()

        // Do any additional setup after loading the view.
    }
    
    func setHeaderView() {
        let headView = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 40))
        headView.addSubview(searchBar)
        headView.addSubview(searchBtn)
        tableView.tableHeaderView = headView
        
        searchBtn.isEnabled = false
    }
    
    func setRecommendView() {
        let recommendView = SearchRecommendView.init(frame: FRAME_CGFloat(0, 100, SCREEN_WIDTH, 200), viewController: self)
        recommendView.delegate = self
        self.view.addSubview(recommendView)
    }
    
    func filterBySubstring(filterStr: NSString!) {
//        isSearch = true
        let predicate = NSPredicate(format: "SELF CONTAINS[c] %@", filterStr)
//        searchArray = tableArray.filtered(using: predicate) as NSArray
        tableView.reloadData()
    }
    
    @objc func searchBtnAction() {
        MTProgressHUD.show()
        searchBar.resignFirstResponder()
        let para = ["condition": searchBar.text ?? "","userId":String.getUserId(),"startPoint":1,"maxPage":999] as [String : Any]
        HomeRequestModel.loadSearch(parameters: para) { (success, message, model) in
            if success && model != nil {
                self.listModelArr = model!
                self.tableView.reloadData()
            } else {
                TTAlertView.showMessage(message: message)
            }
            if self.listModelArr.count == 0 {
                self.tableView.setNoData(nodata: true)
            } else {
                self.tableView.setNoData(nodata: false)
            }
            MTProgressHUD.hidded(true)
        }
    }
}

//  MARK: - ###### data ######
//  MARK: - ###### 协议 ######
extension SearchVC {
    func setData() {
        HomeRequestModel.loadDefaultSeatchWordData { (success, message, model) in
            if success && model != nil {
                self.searchBar.text = model?[0].name
            }
        }
    }
}

//  MARK: - ###### 协议 ######
extension SearchVC: UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, ListenTableViewCellDelegate, SearchRecommendViewDelegate {
    func SearchRecommendViewClick(name: String) {
        self.searchBar.text = name
    }
    
    func playActionDele(cell: UITableViewCell, paly: Bool) {
        let index:IndexPath = self.tableView.indexPath(for: cell) as! IndexPath
        
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listModelArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 95.5 + SCREEN_SCALE_355X(x: 140)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SearchListTableViewCellID) as! ListenTableViewCell
        cell.delegate = self
        cell.modelList = self.listModelArr[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let playVc = PlayVC()
        playVc.id = self.listModelArr[indexPath.row].id
        ISLOGIN_PUSHVIEWCONTROLLER(playVc)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView.init()
    }
    
    // search
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
//        isSearch = false
        searchBar.resignFirstResponder()
        tableView.reloadData()
    }
    
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText == "" {
            searchBtn.isEnabled = false
            searchBtn.setTitleColor(UIColor.lightGray, for: .normal)
        } else {
            searchBtn.isEnabled = true
            searchBtn.setTitleColor(BlackBackColor, for: .normal)
        }
    }
        
}
